﻿using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;

namespace Mekiasm
{

    public class MekCoalGenerator : MachineRotateAbleItem
    {
        public static int Index;

        public MekCoalGenerator() : base("火力发电机")
        {
            textureSlots = new int[] { 114, 112, 112, 112, 112, 112 };
        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }

        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {

            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, new List<ComponentEnergyMachine.Slot>() { //格子初始化
                new ComponentEnergyMachine.Slot(),new ComponentEnergyMachine.Slot(){ FuelCapacity=40960 }
            }, ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item)
            }), new MachineAmountLimitConfig()
            {
                MaxPower = 10240,
                InputPowerLimit = 32,
                OutputPowerLimit = 32,
            });
            ElectricSystem.Add(new CoalGenerateElement(x, y, z));
        }

        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new CoalGenerateElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ILibrary.RemoveBlockEntity(x, y, z);
            ElectricSystem.Remove(new Point3(x, y, z));
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentCoalGenerator mcomponent = ILibrary.SubsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentCoalGenerator>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new CoalGeneratorWidget(mcomponent, componentMiner);
            return true;
        }
    }
}
